<?php
/**
 * 轮播图
 *
 * 轮播图service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\CarouselsService;
use Illuminate\Support\Facades\DB;
use App\Services\ProductsService\ProductsBackService;
use App\Services\ProductTempService\ProductTempService;
use App\Services\ImgServices\ImgTool;
use App\Services\BaseService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\ColourService\ColourTool;
use App\Services\ProductsService\ProductTool;

class CarouselsBackService extends BaseService
{
    public function __construct(ProductsBackService $product_back_aervice,ProductTempService $product_temp_service,
                                ImgTool $img_tool,ProductTempTool $product_temp_tool,ProductTool $product_tool)
    {
        $this->product_back_aervice = $product_back_aervice;
        $this->product_temp_service = $product_temp_service;
        $this->product_temp_tool = $product_temp_tool;
        $this->product_tool = $product_tool;
        $this->img_tool = $img_tool;
    }

    /**
     * 后台获取轮播图
     * @return mixed
     */
    public function getList($per_page = 10){
        $carousels = DB::table('carousels')
            ->where('state','<>',2)
            ->select(
                'carousel_id','carousel_image', 'carousel_type','app_id','resource_id',
                'sort','state','url', 'created_at', 'updated_at','begin','end'
            )
            ->orderBy('sort','desc')
            ->paginate($per_page);
        foreach($carousels as $carousel){
            $carousel->carousel_image = env('IMG_URL_STD_PREFIX').$carousel->carousel_image;
            if($carousel->end > time() && $carousel->begin < time()){
                $carousel->is_end = 1;//正在进行
            }else if($carousel->begin > time()){
                $carousel->is_end = 2;//还未开始
            }else if($carousel->end < time()){
                $carousel->is_end = 0;//已经结束
            }
            //判断轮播图资源状态 0 正常 1 库存不足 2 出售数量 3 下架 4 资源不存在或被删除 5 模板被删除 6 素材或字体存在异常
            $carousel->resource_state = 0;
            if($carousel->carousel_type == 1){
                //产品 检查产品是否下架
                $product = DB::table('products')->where('product_id',$carousel->resource_id)->first();
                $is_product_temp_exit = ProductTempTool::productTempExistsByProdTempId($product->product_temp_id);
                if(empty($product) || $product->state == 2){
                    $carousel->resource_state = 4;
                }elseif($product->is_shelves == 0 || ($product->is_shelves == 1 && ($product->is_infinite_time == 0 && $product->off_shelves_time < time()))){
                    $carousel->resource_state = 3;
                }elseif($product->is_infinite_stock == 0 && $product->stock <= 0){
                    $carousel->resource_state = 2;
                }elseif(!$is_product_temp_exit){
                    $carousel->resource_state = 5;
                }else{
                    $product_temp_sold_out = $this->product_temp_out($product->product_temp_id,$product->colour_id);
                    if($product_temp_sold_out == 1){
                        $carousel->resource_state = 1;
                    }
                    $result = $this->product_tool->is_used_source_and_font_abnormal($product->product_id,$product->product_temp_id);
                    if($result['state'] != 'M00000'){
                        $carousel->resource_state = 6;
                    }
                }
            }elseif($carousel->carousel_type == 2){
                $is_product_temp_exit = ProductTempTool::productTempExistsByProdTempId($carousel->resource_id);
                if(!$is_product_temp_exit){
                    $carousel->resource_state = 5;
                }else{
                    //模板
                    $colour_ids = $this->product_temp_tool->productTempColourIdsByProductTempId($carousel->resource_id,1);
                    $product_temp_all_sold_out = 1;
                    foreach($colour_ids as $colour_id){
                        $product_temp_sold_out = $this->product_temp_out($carousel->resource_id,$colour_id);
                        if($product_temp_sold_out == 0){
                            $product_temp_all_sold_out = 0;
                            break;
                        }
                    }
                    if($product_temp_all_sold_out == 1){
                        $carousel->resource_state = 1;
                    }
                }
            }
        }
        return $carousels;
    }

    /**
     * 判断产品是否售空
     * @param $product_temp_id
     * @param $colour_id
     * @return int
     */
    public function product_temp_out($product_temp_id,$colour_id){
        $product_temp = $this->product_temp_tool->getProdTempDetailByProdTempId($product_temp_id, $colour_id);
        $colour_name = ColourTool::getColourSimpleInfoByColourId($colour_id);
        $size_names = ProductTempTool::getProdTempSizesByProdTempId($product_temp_id);
        if (empty($product_temp) || empty($colour_name) || empty($size_names)) {
            $product_temp_sold_out = 1;
        } else {
            $product_temp_sold_out = 1;
            foreach ($size_names as $size_name) {
                $product_temp_stock = ProductTempTool::getStock($product_temp_id, $colour_name->colour_name, $size_name);
                if (isset($product_temp_stock['stock']) && ($product_temp_stock['stock'] > 0)) {
                    $product_temp_sold_out = 0;//还未售罄
                    break;
                }
            }
        }
        return $product_temp_sold_out;
    }

    /**
     * 存轮播图
     * @param $carousel_image
     * @param $carousel_image_type
     * @param $product_id
     * @param $create_id
     * @return mixed
     */
    public function add_carousel($data){
        $path = $this->img_tool->imgSave($data['carousel_image'],2);
        if(empty($path)){
            return array('state'=>'M00001','result'=>'','message'=>'添加失败');
        }
        $data['created_at'] = $data['updated_at'] = time();
        $data['carousel_image'] = $path;
        $data['begin'] = strtotime($data['begin'].' 00:00:00');
        $data['end'] = strtotime($data['end'].' 23:59:59');
        if($data['end'] <= $data['begin']){
            return array('state'=>'M00001','result'=>'','message'=>'开始时间不能小于结束时间');
        }
        if($data['carousel_type'] == '1'){
            $data['app_id'] = 3;
        }elseif($data['carousel_type'] == '2'){
            $data['app_id'] = 7;
        }elseif($data['carousel_type'] == '3'){
            $data['app_id'] = 0;
        }else{
            $data['app_id'] = 0;
        }
        $flag = DB::table('carousels')->insert($data);
        return $flag ? array('state'=>'M00000','result'=>'','message'=>'添加成功') : array('state'=>'M00001','result'=>'','message'=>'添加失败');
    }

    /**
     * 检查参数
     * @param $data
     * @return array
     */
    public function checkParam($data)
    {
        if(!in_array($data['carousel_type'],array(1,2,3,4))){
            return array('state' => 'M00001','message' => '参数有误，请认真核对');
        }elseif($data['carousel_type'] == '1'){
            $count = $this->product_back_aervice->product_exist($data['resource_id']);
            if(empty($count)){
                return $this->createResult('M08001',(object)array());
            }
        }elseif($data['carousel_type'] == '2'){
            $product_temp = $this->product_temp_service->getProdTempByProdTempId($data['resource_id']);
            if(empty($product_temp)){
                return array('message'=>'未找到产品模板','result'=>(object)array(),'state'=>'M00001');
            }
        }elseif($data['carousel_type'] == '3' && empty($data['url'])) {
            return array('message'=>'请输入外链地址','result'=>(object)array(),'state'=>'M00001');
        }
        return $this->createResult('M00000',(object)array());
    }

    /**
     * 获取轮播图详情
     * @param $carousel_image_id
     * @return mixed
     */
    public function get_carousel_by_id($carousel_id){
        $carousel = DB::table('carousels')
            ->where('carousel_id',$carousel_id)
            ->where('state','<>',2)
            ->first();
        if(!empty($carousel)){
            $carousel->carousel_image = env('IMG_URL_STD_PREFIX').$carousel->carousel_image;
        }
        return $carousel;
    }

    //更新轮播图
    public function update_carousel($data){
        if(!empty($data['carousel_image'])){
            $data['carousel_image'] = $this->img_tool->imgSave($data['carousel_image'],2);
            if(empty($data['carousel_image'])){
                return array('state'=>'M00001','result'=>'','message'=>'更新失败');
            }
        }
        if($data['carousel_type'] == '1'){
            $data['app_id'] = 3;
        }else if($data['carousel_type'] == '2'){
            $data['app_id'] = 7;
        }else if($data['carousel_type'] == '3'){
            $data['app_id'] = 0;
        }else{
            $data['app_id'] = 0;
        }
        $data['updated_at'] = time();
        $data['begin'] = strtotime($data['begin'].' 00:00:00');
        $data['end'] = strtotime($data['end'].' 23:59:59');
        if($data['end'] <= $data['begin']){
            return array('state'=>'M00001','result'=>'','message'=>'开始时间不能小于结束时间');
        }
        $flag = DB::table('carousels')
            ->where('carousel_id',$data['carousel_id'])
            ->update($data);
        return $flag ? array('state'=>'M00000','result'=>'','message'=>'更新成功') : array('state'=>'M00001','result'=>'','message'=>'更新失败');
    }

    /**
     * 更新排序
     * @param $carousel_id
     * @param $sort
     * @return mixed
     */
    public function changeSort($carousel_id,$sort){
        $flag = DB::table('carousels')
            ->where('carousel_id',$carousel_id)
            ->update(['sort'=>$sort,'updated_at'=>time()]);
        return $flag;
    }

    /**
     * 更新状态
     * @param $state
     * @param $id
     * @return mixed
     */
    public function changeState($state,$carousel_id){
        $flag = DB::table('carousels')
            ->where('carousel_id',$carousel_id)
            ->update(['state' => $state,'updated_at'=>time()]);
        return $flag;
    }
}